#include "api/apiinfo.h"
#include <QHostInfo>
#include <QNetworkInterface>
#include <QString>
#include <QUuid>

static long m_connectTimeout = 5000L;//cURL连接超时时长（毫秒）
static long m_timeout = 300000L;//cURL总超时时长（毫秒）
static std::string m_versionNo;
static std::string m_rootUrl;
static std::string m_loginToken;
static std::string m_loginUserId;
static std::string m_loginUserName;
static std::string m_loginUserNameEncoded;
static std::string m_requestIp;
static QString m_mac;
static std::string m_hostName;
static std::string m_activeFuncId;

ApiInfo::ApiInfo() = default;
long ApiInfo::connectTimeout()
{
	return m_connectTimeout;
}
void ApiInfo::setConnectTimeout(const long timeout)
{
	m_connectTimeout = timeout;
	std::cout << "cxi_api:setConnectTimeout " << timeout << std::endl;
}
long ApiInfo::requestTimeout()
{
	return m_timeout;
}
void ApiInfo::setRequestTimeout(const long timeout)
{
	m_timeout = timeout;
	std::cout << "cxi_api:setRequestTimeout " << timeout << std::endl;
}
std::string ApiInfo::getVersionNo()
{
	return m_versionNo;
}
void ApiInfo::setVersionNo(const std::string& versionNo)
{
	m_versionNo = versionNo;
	std::cout << "cxi_api:setVersionNo " << m_versionNo << std::endl;
}
std::string ApiInfo::getRootUrl()
{
	return m_rootUrl;
}
void ApiInfo::setRootUrl(const std::string& rootUrl)
{
	m_rootUrl = rootUrl;
	std::cout << "cxi_api:setRootUrl " << m_rootUrl << std::endl;
}
std::string ApiInfo::getLoginUserId()
{
	return m_loginUserId;
}
std::string ApiInfo::getLoginUserName()
{
	return m_loginUserName;
}
std::string ApiInfo::getLoginUserNameEncoded()
{
	return m_loginUserNameEncoded;
}
std::string ApiInfo::getIp()
{
	return m_requestIp;
}
std::string ApiInfo::getActiveFuncId()
{
	return m_activeFuncId;
}
void ApiInfo::setActiveFuncId(const std::string& activeFuncId)
{
	m_activeFuncId = activeFuncId;
}
void ApiInfo::setInfoAfterLogin(const std::string& token, const std::string& loginIp, const std::string& userId, const std::string& userName)
{
	m_loginToken = token;
	m_requestIp = loginIp;
	m_loginUserId = userId;
	setLoginUserName(userName);
}
void ApiInfo::setLoginToken(const std::string& loginToken)
{
	m_loginToken = loginToken;
}
std::string ApiInfo::getLoginToken()
{
	return m_loginToken;
}
void ApiInfo::setLoginUserId(const std::string& loginUserId)
{
	m_loginUserId = loginUserId;
}
void ApiInfo::setLoginUserName(const std::string& loginUserName)
{
	m_loginUserName = loginUserName;
	//将LoginUserName进行UrlEncode编码
	if (m_loginUserName.empty())
	{
		m_loginUserNameEncoded = "";
	}
	else
	{
		const QByteArray byteArray = QString::fromStdString(m_loginUserName).toUtf8();
		const QByteArray byteArrayEncoded = byteArray.toPercentEncoding();
		m_loginUserNameEncoded = QString(byteArrayEncoded).toStdString();
	}
}
std::string ApiInfo::getGuid()
{
	QString uuid = QUuid::createUuid().toString(QUuid::Id128);
	if (m_mac.isEmpty())
	{
		//获取所有网络接口列表
		QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();
		const int nCnt = nets.count();
		for (int i = 0; i < nCnt; i++)
		{
			//如果此网络接口被激活并且正在运行并且不是回环地址，且该mac的ip地址不能是回环地址并且是ipv4地址，则就是我们需要找的Mac地址
			if (const QNetworkInterface& net = nets[i]; net.flags().testFlag(QNetworkInterface::IsUp) && net.flags().testFlag(QNetworkInterface::IsRunning) && net.flags().testFlag(QNetworkInterface::IsLoopBack))
			{
				for (int j = 0; j < net.addressEntries().size(); j++)
				{
					//该mac的ip地址不能为127.0.0.1，且需是ipv4地址
					if (QNetworkAddressEntry nae = net.addressEntries().at(j); nae.ip() != QHostAddress::LocalHost && nae.ip().protocol() == QAbstractSocket::IPv4Protocol)
					{
						m_mac = net.hardwareAddress();
						break;
					}
				}
			}

			if (!m_mac.isEmpty())
			{
				break;
			}
		}
	}

	return uuid.append(m_mac).toStdString();
}
std::string ApiInfo::getHostName()
{
	if (m_hostName.empty())
	{
		m_hostName = QHostInfo::localHostName().toStdString();
	}

	return m_hostName;
}
void ApiInfo::setIp(const std::string& ip)
{
	m_requestIp = ip;
}